package example.linklist_array.断层合并;

public class Demo1 {

    public static ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        ListNode temp = list1;
        //a-prev
        ListNode tempPrev = null;
        for (int i = 0; i < a; i++) {
            tempPrev = temp;
            temp = temp.next;
        }
        //b
        for (int i = a; i < b; i++) {
            temp = temp.next;
        }
        ListNode temp2Prev = null;
        ListNode temp2 = list2;
        while (temp2 != null) {
            temp2Prev = temp2;
            temp2 = temp2.next;
        }
        tempPrev.next = list2;
        temp2Prev.next = temp.next;
        return list1;
    }

    public static void main(String[] args) {

    }


    //    Definition for singly-linked list.
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

}
